﻿using ACC.Extensions.ClientBusiness;
using ACC.Extensions.Entities;
using ACC.Extensions.PCClient.Helper;
using MahApps.Metro.Controls;
using System;
using System.Windows;

namespace ACC.Extensions.PCClient.View
{
    /// <summary>
    /// Interaction logic for ConfigClientWnd.xaml
    /// </summary>
    public partial class ConfigClientWnd : MetroWindow
    {
        public ConfigClientWnd()
        {
            InitializeComponent();
            this.Owner = Application.Current.MainWindow;
        }
        private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
        {
            if (UISession.ClientSetting != null)
            {
                //this.chkAutoLoad.IsChecked = UISession.ClientSetting.AutoLoad;
                //this.chkAutoUnload.IsChecked = UISession.ClientSetting.AutoUnload;
                this.chkIsFirstOP.IsChecked = UISession.ClientSetting.IsFirstOP;
                //this.chkLineLeaderAssign.IsChecked = UISession.ClientSetting.UseLineLeaderAssign;
                this.chkNeedOperatorLogin.IsChecked = UISession.ClientSetting.NeedOperatorLogin;
                this.chkUseUnloadPallet.IsChecked = UISession.ClientSetting.UseUnloadPallet;
                this.chkMaterialInOut.IsChecked = UISession.ClientSetting.UseMaterialInOut;
                this.chkCoreMaterial.IsChecked = UISession.ClientSetting.IsCoreMaterial;
                //this.chkNeedPreLoad.IsChecked = UISession.ClientSetting.NeedPreLoad;
                //this.chkDisplayScanNo.IsChecked = UISession.ClientSetting.IsDisplayScanNo;
                //this.chkDisplayCodeLevel.IsChecked = UISession.ClientSetting.IsDisplayCodeLevel;
                //this.chkWndBasketBox.IsChecked = UISession.ClientSetting.IsUseBasketBoxGrid;
                //this.chkFirstInFirstOut.IsChecked = UISession.ClientSetting.IsFirstInFirstOut;

                //this.chkUIGridPic.IsChecked = UISession.ClientSetting.IsGridPicUI;
                //this.chkUIHardness.IsChecked = UISession.ClientSetting.IsHardnessUI;
                //this.chkUIProducePlan.IsChecked = UISession.ClientSetting.IsProducePlanUI;
                this.chkOKBtn.IsChecked = UISession.ClientSetting.IsOKBtn;
                this.chkNOKBtn.IsChecked = UISession.ClientSetting.IsNOKBtn;
                //this.chkScannerBtn.IsChecked = UISession.ClientSetting.IsScannerBtn;
                //this.chkBarcodeListBtn.IsChecked = UISession.ClientSetting.IsBarcodeListBtn;

                this.chkEnablePLC.IsChecked = UISession.ClientSetting.EnablePLC;
                this.txtPlcDescription.Text = UISession.ClientSetting.PLCDescription;
                this.txtIP.Text = UISession.ClientSetting.PLC_IP;
                this.txtPort.Text = UISession.ClientSetting.PLC_Port;
                this.txtRack.Text = UISession.ClientSetting.PLC_Rack;
                this.txtSlot.Text = UISession.ClientSetting.PLC_Slot;

                //this.chkEnableIP1.IsChecked = UISession.ClientSetting.EnableIP1;
                //this.txtIPAddress1.Text = UISession.ClientSetting.IPAddress1;
                //this.txtPort1.Text = UISession.ClientSetting.Port1;
                //this.txtIPDescription1.Text = UISession.ClientSetting.IPDescription1;

                //this.chkEnableIP2.IsChecked = UISession.ClientSetting.EnableIP2;
                //this.txtIPAddress2.Text = UISession.ClientSetting.IPAddress2;
                //this.txtPort2.Text = UISession.ClientSetting.Port2;
                //this.txtIPDescription2.Text = UISession.ClientSetting.IPDescription2;

                //this.chkEnableIP3.IsChecked = UISession.ClientSetting.EnableIP3;
                //this.txtIPAddress3.Text = UISession.ClientSetting.IPAddress3;
                //this.txtPort3.Text = UISession.ClientSetting.Port3;
                //this.txtIPDescription3.Text = UISession.ClientSetting.IPDescription3;

                this.chkEnablePLCDB1.IsChecked = UISession.ClientSetting.EnablePLCDB1;
                this.txtPLCDBAddress1.Text = UISession.ClientSetting.PLCDBAddress1;
                this.txtPLCDBDescription1.Text = UISession.ClientSetting.PLCDBDescription1;

                this.chkEnablePLCDB2.IsChecked = UISession.ClientSetting.EnablePLCDB2;
                this.txtPLCDBAddress2.Text = UISession.ClientSetting.PLCDBAddress2;
                this.txtPLCDBDescription2.Text = UISession.ClientSetting.PLCDBDescription2;

                this.chkEnablePLCDB3.IsChecked = UISession.ClientSetting.EnablePLCDB3;
                this.txtPLCDBAddress3.Text = UISession.ClientSetting.PLCDBAddress3;
                this.txtPLCDBDescription3.Text = UISession.ClientSetting.PLCDBDescription3;

                this.chkEnablePLCDB4.IsChecked = UISession.ClientSetting.EnablePLCDB4;
                this.txtPLCDBAddress4.Text = UISession.ClientSetting.PLCDBAddress4;
                this.txtPLCDBDescription4.Text = UISession.ClientSetting.PLCDBDescription4;

                this.chkEnablePLCDB5.IsChecked = UISession.ClientSetting.EnablePLCDB5;
                this.txtPLCDBAddress5.Text = UISession.ClientSetting.PLCDBAddress5;
                this.txtPLCDBDescription5.Text = UISession.ClientSetting.PLCDBDescription5;

                this.chkEnablePLCDB6.IsChecked = UISession.ClientSetting.EnablePLCDB6;
                this.txtPLCDBAddress6.Text = UISession.ClientSetting.PLCDBAddress6;
                this.txtPLCDBDescription6.Text = UISession.ClientSetting.PLCDBDescription6;

                this.chkEnablePLCDB7.IsChecked = UISession.ClientSetting.EnablePLCDB7;
                this.txtPLCDBAddress7.Text = UISession.ClientSetting.PLCDBAddress7;
                this.txtPLCDBDescription7.Text = UISession.ClientSetting.PLCDBDescription7;

                //this.txtDefectLevel.Text = UISession.ClientSetting.DefectLevel.ToString();
           
                //this.chkUseCacheGrid.IsChecked = UISession.ClientSetting.IsUseCahceGrid;
                //this.chkUseUnitStatus.IsChecked = UISession.ClientSetting.IsUseUniform;
                this.chkRepairBtn.IsChecked = UISession.ClientSetting.IsRepairBtn;
                //this.chkResultWithReason.IsChecked = UISession.ClientSetting.ResultWithReason;
                //this.chkResultWithGridPic.IsChecked = UISession.ClientSetting.ResultWithGridPic;
                //this.chkResultWithGridPicAutoPopup.IsChecked = UISession.ClientSetting.ResultWithGridPicAutoPopup;
                //this.chkWndProducePlan.IsChecked = UISession.ClientSetting.IsUseProducePlanGrid;

                this.txtPortName.Text = UISession.ClientSetting.COM_PortName;
                this.txtBaudRate.Text = UISession.ClientSetting.COM_BaudRate;
                this.txtParity.Text = UISession.ClientSetting.COM_Parity;
                this.txtDataBits.Text = UISession.ClientSetting.COM_DataBits;
                this.txtStopBits.Text = UISession.ClientSetting.COM_StopBits;
                this.chkEnableCOM.IsChecked = UISession.ClientSetting.COM_Enable;
               
            }
        }
        private void btnConfirm_Click(object sender, RoutedEventArgs e)
        {
            if (UISession.ClientSetting == null)
            {
                UISession.ClientSetting = new LocalSetting();
            }
            //UISession.ClientSetting.AutoLoad =
            //    chkAutoLoad.IsChecked.HasValue ? chkAutoLoad.IsChecked.Value : false;
            //UISession.ClientSetting.AutoUnload =
            //    chkAutoUnload.IsChecked.HasValue ? chkAutoUnload.IsChecked.Value : false;
            //UISession.ClientSetting.NeedPreLoad =
            //    chkNeedPreLoad.IsChecked.HasValue ? chkNeedPreLoad.IsChecked.Value : false;
            UISession.ClientSetting.NeedOperatorLogin =
                chkNeedOperatorLogin.IsChecked.HasValue ? chkNeedOperatorLogin.IsChecked.Value : false;
            UISession.ClientSetting.UseUnloadPallet =
                chkUseUnloadPallet.IsChecked.HasValue ? chkUseUnloadPallet.IsChecked.Value : false;
            UISession.ClientSetting.UseMaterialInOut =
                chkMaterialInOut.IsChecked.HasValue ? chkMaterialInOut.IsChecked.Value : false;
            UISession.ClientSetting.IsCoreMaterial =
                chkCoreMaterial.IsChecked.HasValue ? chkCoreMaterial.IsChecked.Value : false;
            //UISession.ClientSetting.UseLineLeaderAssign =
            //    chkLineLeaderAssign.IsChecked.HasValue ? chkLineLeaderAssign.IsChecked.Value : false;
            UISession.ClientSetting.IsFirstOP =
                chkIsFirstOP.IsChecked.HasValue ? chkIsFirstOP.IsChecked.Value : false;
            //UISession.ClientSetting.IsDisplayScanNo =
            //    chkDisplayScanNo.IsChecked.HasValue ? chkDisplayScanNo.IsChecked.Value : false;
            //UISession.ClientSetting.IsDisplayCodeLevel =
            //   this.chkDisplayCodeLevel.IsChecked.HasValue ? chkDisplayCodeLevel.IsChecked.Value : false;
            //UISession.ClientSetting.IsUseBasketBoxGrid =
            //    chkWndBasketBox.IsChecked.HasValue ? chkWndBasketBox.IsChecked.Value : false;
            //UISession.ClientSetting.IsFirstInFirstOut =
            //    chkFirstInFirstOut.IsChecked.HasValue ? chkFirstInFirstOut.IsChecked.Value : false;

            //UISession.ClientSetting.IsGridPicUI = this.chkUIGridPic.IsChecked.Value ? this.chkUIGridPic.IsChecked.Value : false;
            //UISession.ClientSetting.IsHardnessUI = this.chkUIHardness.IsChecked.Value?this.chkUIHardness.IsChecked.Value:false;
            //UISession.ClientSetting.IsProducePlanUI = this.chkUIProducePlan.IsChecked.Value ? this.chkUIProducePlan.IsChecked.Value : false;
            UISession.ClientSetting.IsOKBtn = this.chkOKBtn.IsChecked.Value ? this.chkOKBtn.IsChecked.Value : false;
            UISession.ClientSetting.IsNOKBtn = this.chkNOKBtn.IsChecked.Value ? this.chkNOKBtn.IsChecked.Value : false;
            //UISession.ClientSetting.IsScannerBtn = this.chkScannerBtn.IsChecked.Value ? this.chkScannerBtn.IsChecked.Value : false;
            //UISession.ClientSetting.IsBarcodeListBtn = this.chkBarcodeListBtn.IsChecked.Value ? this.chkBarcodeListBtn.IsChecked.Value : false;

            UISession.ClientSetting.EnablePLC = this.chkEnablePLC.IsChecked.HasValue ? this.chkEnablePLC.IsChecked.Value : false;
            UISession.ClientSetting.PLCDescription = this.txtPlcDescription.Text.Trim();
            UISession.ClientSetting.PLC_IP = this.txtIP.Text.Trim();
            UISession.ClientSetting.PLC_Port = this.txtPort.Text.Trim();
            UISession.ClientSetting.PLC_Rack = this.txtRack.Text.Trim();
            UISession.ClientSetting.PLC_Slot = this.txtSlot.Text.Trim();

            //UISession.ClientSetting.EnableIP1 = this.chkEnableIP1.IsChecked.HasValue ? this.chkEnableIP1.IsChecked.Value : false;
            //UISession.ClientSetting.IPAddress1 = this.txtIPAddress1.Text.Trim();
            //UISession.ClientSetting.Port1 = this.txtPort1.Text.Trim();
            //UISession.ClientSetting.IPDescription1 = this.txtIPDescription1.Text.Trim();

            //UISession.ClientSetting.EnableIP2 = this.chkEnableIP2.IsChecked.HasValue ? this.chkEnableIP2.IsChecked.Value : false;
            //UISession.ClientSetting.IPAddress2 = this.txtIPAddress2.Text.Trim();
            //UISession.ClientSetting.Port2 = this.txtPort2.Text.Trim();
            //UISession.ClientSetting.IPDescription2 = this.txtIPDescription2.Text.Trim();

            //UISession.ClientSetting.EnableIP3 = this.chkEnableIP3.IsChecked.HasValue ? this.chkEnableIP3.IsChecked.Value : false;
            //UISession.ClientSetting.IPAddress3 = this.txtIPAddress3.Text.Trim();
            //UISession.ClientSetting.Port3 = this.txtPort3.Text.Trim();
            //UISession.ClientSetting.IPDescription3 = this.txtIPDescription3.Text.Trim();

            UISession.ClientSetting.EnablePLCDB1 = this.chkEnablePLCDB1.IsChecked.HasValue ? this.chkEnablePLCDB1.IsChecked.Value : false;
            UISession.ClientSetting.PLCDBAddress1 = this.txtPLCDBAddress1.Text.Trim(); 
            UISession.ClientSetting.PLCDBDescription1 = this.txtPLCDBDescription1.Text.Trim();

            UISession.ClientSetting.EnablePLCDB2 = this.chkEnablePLCDB2.IsChecked.HasValue ? this.chkEnablePLCDB2.IsChecked.Value : false;
            UISession.ClientSetting.PLCDBAddress2 = this.txtPLCDBAddress2.Text.Trim();
            UISession.ClientSetting.PLCDBDescription2 = this.txtPLCDBDescription2.Text.Trim();

            UISession.ClientSetting.EnablePLCDB3 = this.chkEnablePLCDB3.IsChecked.HasValue ? this.chkEnablePLCDB3.IsChecked.Value : false;
            UISession.ClientSetting.PLCDBAddress3 = this.txtPLCDBAddress3.Text.Trim();
            UISession.ClientSetting.PLCDBDescription3 = this.txtPLCDBDescription3.Text.Trim();

            UISession.ClientSetting.EnablePLCDB4 = this.chkEnablePLCDB4.IsChecked.HasValue ? this.chkEnablePLCDB4.IsChecked.Value : false;
            UISession.ClientSetting.PLCDBAddress4 = this.txtPLCDBAddress4.Text.Trim();
            UISession.ClientSetting.PLCDBDescription4 = this.txtPLCDBDescription4.Text.Trim();

            UISession.ClientSetting.EnablePLCDB5 = this.chkEnablePLCDB5.IsChecked.HasValue ? this.chkEnablePLCDB5.IsChecked.Value : false;
            UISession.ClientSetting.PLCDBAddress5 = this.txtPLCDBAddress5.Text.Trim();
            UISession.ClientSetting.PLCDBDescription5 = this.txtPLCDBDescription5.Text.Trim();

            UISession.ClientSetting.EnablePLCDB6 = this.chkEnablePLCDB6.IsChecked.HasValue ? this.chkEnablePLCDB6.IsChecked.Value : false;
            UISession.ClientSetting.PLCDBAddress6 = this.txtPLCDBAddress6.Text.Trim();
            UISession.ClientSetting.PLCDBDescription6 = this.txtPLCDBDescription6.Text.Trim();

            UISession.ClientSetting.EnablePLCDB7 = this.chkEnablePLCDB7.IsChecked.HasValue ? this.chkEnablePLCDB7.IsChecked.Value : false;
            UISession.ClientSetting.PLCDBAddress7 = this.txtPLCDBAddress7.Text.Trim();
            UISession.ClientSetting.PLCDBDescription7 = this.txtPLCDBDescription7.Text.Trim();

            //UISession.ClientSetting.DefectLevel = string.IsNullOrEmpty(this.txtDefectLevel.Text) ? 9999 : int.Parse(this.txtDefectLevel.Text.Trim());

            //UISession.ClientSetting.IsUseCahceGrid = this.chkUseCacheGrid.IsChecked.HasValue ? chkUseCacheGrid.IsChecked.Value : false;
            //UISession.ClientSetting.IsUseUniform = chkUseUnitStatus.IsChecked.HasValue ? chkUseUnitStatus.IsChecked.Value : false;
            UISession.ClientSetting.IsRepairBtn = this.chkRepairBtn.IsChecked.HasValue ? chkRepairBtn.IsChecked.Value : false;
            //UISession.ClientSetting.ResultWithReason = chkResultWithReason.IsChecked.HasValue ? chkResultWithReason.IsChecked.Value : false;
            //UISession.ClientSetting.ResultWithGridPic = chkResultWithGridPic.IsChecked.HasValue ? chkResultWithGridPic.IsChecked.Value : false;
            //UISession.ClientSetting.ResultWithGridPicAutoPopup = chkResultWithGridPicAutoPopup.IsChecked.HasValue ? chkResultWithGridPicAutoPopup.IsChecked.Value : false;
            //UISession.ClientSetting.IsUseProducePlanGrid = chkWndProducePlan.IsChecked.HasValue ? chkWndProducePlan.IsChecked.Value : false;

            UISession.ClientSetting.COM_PortName = this.txtPortName.Text;
            UISession.ClientSetting.COM_BaudRate = this.txtBaudRate.Text;
            UISession.ClientSetting.COM_Parity = this.txtParity.Text;
            UISession.ClientSetting.COM_DataBits = this.txtDataBits.Text;
            UISession.ClientSetting.COM_StopBits = this.txtStopBits.Text;
            UISession.ClientSetting.COM_Enable = this.chkEnableCOM.IsChecked.HasValue ? this.chkEnableCOM.IsChecked.Value : false;

            //检查配置文件是否存在
            bool result = ClientSettingHelper.WriteSetting(UISession.ClientSetting);
            if (result)
            {
                this.DialogResult = true;
                this.Close();
            }
            else
            {
                MessageDialogWnd.ShowDialog( LanguageHelper.LoadStringFromResource("TITLE_ERROR"), LanguageHelper.LoadStringFromResource("MSG_ERR_SAVESETTING"));
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        { 
            this.Close();
        }

        private void MetroWindow_Activated(object sender, EventArgs e)
        {
            UISession.IsWndActivated = this.IsActive;
        }

        private void MetroWindow_Deactivated(object sender, EventArgs e)
        {
            UISession.IsWndActivated = this.IsActive;
        }
    }
}
